package com.jshongli.myapplication.common.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;

import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.text.Spanned;
import android.util.DisplayMetrics;
import android.widget.EditText;
import android.widget.TextView;


public class TextViewUtils {

    public static String excludeNull(String string) {
        if (string == null || string.length() == 0 || string.equals("null")) {
            return "-";
        } else {
            return string;
        }
    }

    public static Spanned setColors(String[] texts, String[] colors){

        String combine = "";

        for (int i = 0, len = texts.length;i < len;i++) {

            String text = "<font color=\"" + colors[i] + "\">" + texts[i] + "</font>";
            combine = combine + text;
        }

        return Html.fromHtml(combine);

    }

    public static float getTextSize(Context context, int resId) {

        DisplayMetrics dm = new DisplayMetrics();
        ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(dm);

        float textSize = context.getResources().getDimension(resId) / dm.density;

        return textSize;
    }

    /**
     * 给TextView设置drawable
     *
     * @param context
     * @param textView
     * @param res      图片资源
     * @param i        位置  0 ：left  1 ：top  2 ：right  3：bottom
     */
    public static void setNormalTvDrawable(Context context, TextView textView, int res, int i) {
        Drawable drawable = ContextCompat.getDrawable(context, res);
        drawable.setBounds(0, 0, (int) textView.getTextSize(), (int) textView.getTextSize()); // 设置边界
        if (i == 0) {
            textView.setCompoundDrawables(drawable, null, null, null);
        } else if (i == 1) {
            textView.setCompoundDrawables(null, drawable, null, null);
        } else if (i == 2) {
            textView.setCompoundDrawables(null, null, drawable, null);
        } else if (i == 3) {
            textView.setCompoundDrawables(null, null, null, drawable);
        }
    }

    /**
     * EditText
     *
     * @param context
     * @param editText
     * @param res      图片资源
     * @param i        位置  0 ：left  1 ：top  2 ：right  3：bottom
     */
    public static void setNormalEtDrawable(Context context, EditText editText, int res, int i) {
        Drawable drawable = ContextCompat.getDrawable(context, res);
        drawable.setBounds(0, 0, (int) editText.getTextSize(), (int) editText.getTextSize()); // 设置边界
        if (i == 0) {
            editText.setCompoundDrawables(drawable, null, null, null);
        } else if (i == 1) {
            editText.setCompoundDrawables(null, drawable, null, null);
        } else if (i == 2) {
            editText.setCompoundDrawables(null, null, drawable, null);
        } else if (i == 3) {
            editText.setCompoundDrawables(null, null, null, drawable);
        }
    }

}
